package org.ccay.web.support.servlet;

import java.io.IOException;

import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.ccay.core.exception.ApplicationException;
import org.ccay.core.ioc.Ccay;
import org.ccay.core.request.impl.RequestContext;
import org.ccay.core.util.JsonUtil;
import org.ccay.core.util.MimeTypeUtil;
import org.ccay.web.support.WorkspaceVO;
import org.ccay.web.support.filter.ResponseUtil;
import org.ccay.web.support.service.IHomeService;

/**
 * 读取环境变量参数的serlvet，并将环境变量通过js文件输出到客户端，浏览器可以
 * 直接读取workspaceVO 对象来访问 服务端的环境变量与公用参数
 */
@Named("/servlet/environment")
public class ReadEnvironmentServlet extends HttpServlet { 
	private static final long serialVersionUID = 9059315236667440754L;

	
	/** 该方法返回一个定义了js变量的js文件
	 */
	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException,
			IOException {
	
	response.setContentType(MimeTypeUtil.getTypeBySuffix("js")); 
	
		IHomeService workspaceService =(IHomeService)Ccay.getContext().getBean("homeService");
		//设置文件头
		response.setContentType(MimeTypeUtil.getTypeBySuffix("js")); 
		try {
			boolean nomenu = request.getParameter("nomenu") != null;
			WorkspaceVO wvo=workspaceService.findWorkspaceInfo(nomenu);
			wvo.setNetwork(RequestContext.getCurrent().getApplication().getNetwork());
			wvo.getEnvBean().setAppName(RequestContext.getCurrent().getApplication().getAppName());
			//输出内容
			response.getOutputStream().print("workspaceVO=");
			JsonUtil.writeJsonToStream(wvo, response.getOutputStream());
		} catch (ApplicationException e) {
			throw new ServletException(e);
		}	
	}
}

